_package("caf.ui");

_import("caf.mui.Dialog");

/**
 * 消息对话框组件
 */
_class("MessageBox", Dialog, function(){
	var tpl = '<div class="{$cls}">'
		+   '<div class="mbox">'
		+     '<div class="title">{$title}</div>'
		+     '<div class="message">'
		+       '<p>{$msg}</p>'
		+       '<button class="mes-button" _action="dlg_ok">确定</button>'
		+     '</div>'
		+   '</div>'
		+ '</div>';
	/**
	 * @param {String} title 标题
	 * @param {String} message 消息内容
	 * @param {String} layout 默认按钮颜色
	 */
	this._init = function(title, message, layout){
		_super._init.call(this);
		this._params = {
			"title" : title,
			"msg"   : message,
			"layout": layout
		};
		this._id = "";
		this._title = title || "标题";
		this._message = message || "内容";
		this._button = null;
		this._layout = layout || "";
	};
	/**
	 * 创建消息框组件
	 * @param {HTMLElement} parent 父元素
	 * @param {AppBase} app 所属应用
	 * @param {Object} params 参数
	 */
	this.create = function(parent, app, params){
		//_super.create.apply(this, arguments);
		this.setParent(parent);
		this.setApp(app);
		if(params) this.setParams(params);
		this.setId(this._id);
		var hash = {
			"cls"  : "caf-messagebox",
			"title": this._params.title,
			"msg"  : this._params.msg
		};
		var obj = this.createDomElement(parent, runtime.formatTpl(tpl, hash));
		this.init(obj);
		return obj;
	};
	/**
	 * 将消息框的DOM元素绑定到组件内部
	 * @param {HTMLElement} obj DOM元素
	 * @param {Object} conf 配置数据
	 */
	this.bind = function(obj, conf){
		this._id = conf.id;
		this._text = conf.text;
		this._conf = conf;
		this.setParent(conf.parent);
		if(conf.id){
			this.setId(conf.id);
		}
		this.init(obj);
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		this._button = this.find(".mes-button");
		if(this._layout == "orange"){
			this._button.className = "mes-button orange";
		}
		//this.onShow();
		this.addListener(this._button, "tap", this, "do_dlg_ok");
	};
	/**
	 * @param {Object} params 传递给消息框的参数
	 */
	this.reset = function(params){
		_super.reset.apply(this, arguments);
		this._title = params.title;
		this._message = params.msg;
		this.find(".title").innerHTML = this._title;
		this.find(".message p").innerHTML = this._message;
	};
	this.dispose = function(){
		this._button = null;
		this._message = "";
		this._title = "";
		this._id = "";
		_super.dispose.apply(this);
	};
	this.focus = function(){
		this._button.focus();
	};
	/**
	 * 隐藏消息框组件
	 */
	this.hide = function(){
		this.setVisible(false);
		/*
		var parent = this._parent.getContainer();
		var nodes = parent.childNodes;
		var a = [];
		for(var i = 0, len = nodes.length; i < len; i++){
			a.push(nodes[i]);
		}
		nodes = a;
		for(var i = 0, len = nodes.length; i < len; i++){
			var node = nodes[i];
			if(node.className == "caf-messagebox"){
				parent.removeChild(node);
			}
		}
		*/
	};
	/**
	 * 消息框显示时触发事件
	 */
	this.onShow = function(ev){
		this.find(".mbox").style.left = (this._parent.getContainer().clientWidth / 2 - 175) + "px";
	};
	this.do_dlg_ok = function(/*act, sender*/){
		var act = "dlg_ok";
		this.hide();
		runtime._task.execute(this._cbid, [this, act]);  //执行回调函数
	};
});